<template>
  <el-dialog
    :close-on-click-modal="false"
    :visible.sync="visible"
    class="template-dialog"
    title="选择评语模板"
    width="800px"
  >
    <el-dialog width="400px" title="确认选择" :visible.sync="innerVisible" append-to-body>
      <span>确定选择{{ mdl.commentTemplateName }}模板么？</span>
      <div slot="footer">
        <el-button @click="innerVisible = false">取消</el-button>
        <el-button @click="handleSureThis" type="primary">确定</el-button>
      </div>
    </el-dialog>
    <el-form class="mb20" :model="form" ref="form" :inline="true">
      <el-form-item label="评语模板名称">
        <el-input v-model="form.commentTemplateName" placeholder="根据评语模板名称搜索" />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleSearch" type="primary">搜索</el-button>
      </el-form-item>
    </el-form>
    <e-table
      class="eval-table"
      ref="eTable"
      @getList="handleGetList"
      :tableData="tableData"
      :columns="columns"
      :loading="loading"
      :total="total"
    >
      <!-- <el-table-column slot="selection" type="selection" width="55" align="center" /> -->
      <el-table-column label="考评环节" slot="moralityStep" width="110" align="center">
        <template slot-scope="scope">{{ computedCycle( scope.row.moralityStep ) }}</template>
      </el-table-column>
      <el-table-column slot="action" label="操作" width="80" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="handleChoose( scope.row )">选择</el-button>
        </template>
      </el-table-column>
    </e-table>
  </el-dialog>
</template>

<script>
import { templatePage, templateDelete } from '@/api/morality'
export default {
  data() {
    return {
      visible: false,
      form: {
        commentTemplateName: null
      },
      innerVisible: false,
      tableData: [],
      loading: false,
      columns: [
        {
          width: '200px',
          tooltip: true,
          label: '评语模板名称',
          prop: 'commentTemplateName'
        },
        { slot: 'moralityStep' },
        {
          label: '评语',
          prop: 'commentContent',
          tooltip: true
        },
        { slot: 'action' }
      ],
      total: 1,
      queryParam: {
        pageNo: 1,
        pageSize: 10
      },
      selectedRows: [],
      mdl: {}
    }
  },
  // mounted() {
  //   this.handleGetList()
  // },
  methods: {
    computedCycle(num) {
      let str = ''
      switch (num) {
        case 1:
          str = '自评'
          break
        case 2:
          str = '科室'
          break
        case 3:
          str = '党支部'
          break
        case 4:
          str = '考评委员会'
          break
        default:
          break
      }
      return str
    },
    handleSearch() {
      this.handleGetList(this.form)
    },
    show(num) {
      this.visible = true
      this.handleGetList({ moralityStep: num })
    },
    async handleGetList(param = {}) {
      this.loading = true
      this.queryParam = Object.assign(this.queryParam, param)
      const res = await templatePage(this.queryParam)
      if (res && res.data.records.length > 0) {
        const {
          state,
          data: { records, total }
        } = res
        this.tableData = records
        this.total = total
      } else {
        this.tableData = []
        this.total = 1
      }
      this.loading = false
    },
    handleSureThis() {
      this.innerVisible = false
      this.visible = false
      this.$emit('chooseTemplate', this.mdl)
    },
    handleChoose(row) {
      this.innerVisible = true
      this.mdl = Object.assign({}, row)
    }
  }
}
</script>

<style scoped>
.template-dialog >>> .el-dialog__body {
  overflow: hidden;
}
</style>
